<?php

//------------------------
// 商品类型控制器
//-------------------------
namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\GoodsType as GoodsTypeModel;
use app\common\model\TypeAttr as TypeAttrModel;
use think\Config;
use think\Db;

class GoodsType extends AdminBase
{
    protected $GoodsTypeModel;
    protected $TypeAttrModel;

    protected function _initialize()
    {
        parent::_initialize();
        $this->GoodsTypeModel               = new GoodsTypeModel();
        $this->TypeAttrModel                = new TypeAttrModel();
    }


    // 品牌列表
    public function index(){
        $type_name    = $this->request->param('type_name','','trim');

        $where = array();

        if ($type_name) {
             $where['type_name'] = array('like','%'.$type_name.'%');
        }

        $info = $this->GoodsTypeModel->where($where)->order('seq')->
          paginate(10,false,['query' => [
            'type_name'  =>$type_name
            ] ]);

        $this->assign('type_name',$type_name);
        $this->assign('info',$info);

        return $this->fetch('index');
    }


    // 添加页面
    function add(){
        return $this->fetch('add');
    }

    /**
     * @desc 添加操作
     */
    public function addProc() {
        $data            = $this->request->param();

        $validate_result = $this->validate($data, 'GoodsType');

        if ($validate_result !== true) {
                $this->error($validate_result);
        }

        $addData = array(
            'type_name'        => $data['type_name'],
            'seq'               => $data['seq']
        );

        $res = $this->GoodsTypeModel->save($addData);

        if ($res === false) {
            $this->error($this->GoodsTypeModel->getError());
        } else {
            $this->success('添加成功');
        }
    }

    
    // 编辑页面
    function edit(){
        $id = $this->request->param('id',0,'intval');

        $data = $this->GoodsTypeModel->get($id);

        $this->assign('data',$data);
        return $this->fetch('edit');
    }

    // 编辑操作
    function editProc(){
        $data  = $this->request->param('');

        $validate_result = $this->validate($data, 'GoodsType');

        if ($validate_result !== true) {
                $this->error($validate_result);
        }

        $res = $this->GoodsTypeModel->allowField(true)->save($data,$data['id']);

        if ($res === false) {
            $this->error($this->GoodsTypeModel->getError());
        } else {
            $this->success('保存成功');
        }
    }



    // 删除操作
    public function delete(){
        $id = $this->request->param('id',0,'intval');

        $where['id'] = array('eq',$id);

        $res = $this->GoodsTypeModel->where($where)->delete();

        if ($res == false) {
            $this->error($this->GoodsTypeModel->getError());
        } else{
            // 删除成功后

            // 删除类型下面的属性
            Db('TypeAttr')->where('type_id',$id)->delete();

            //删除类型下面的规格下面的规格项
            $ids = Db('TypeSpec')->where('type_id',$id)->field('id')->select();
            $ids = two_to_one($ids,'id');
            $ids = implode(',',$ids);
            Db('SpecItem')->where('spec_id','in',$ids)->delete();

            // 删除类型下面的规格
            Db('TypeSpec')->where('type_id',$id)->delete();

            $this->success('删除成功');
        }
    }

    








}